<template>
    <div class="main coupon-edit">
        <section>
            <el-form  size="medium" ref="form" label-width="150px" :rules="commonRules" :model="busArticle">
                <div class="block-header"><div>文章内容</div></div>
                <div class="block-content">
                    <el-form-item label="文章标题:" prop="title" :rules="commonRules.notNullRule">
                        <el-input v-model="busArticle.title" placeholder="请输入文章标题,不超过50字" maxlength="50"></el-input>
                    </el-form-item>
                    <el-form-item label="副标题:" prop="secondTitle" :rules="commonRules.notNullRule">
                        <el-input v-model="busArticle.secondTitle" placeholder="请输入副标题,不超过50字" maxlength="50"></el-input>
                    </el-form-item>
                    <el-form-item label="频道:" prop="channelId" :rules="commonRules.notNullRule">
                        <el-select v-model="busArticle.channelId" @change="changeChannel" clearable>
                            <!-- <el-option label="素材" :value="1"></el-option> -->
                            <el-option label="商学院" :value="2"></el-option>
                            <el-option label="进阶学习" :value="3"></el-option>
                            <el-option label="邀请海报" :value="4"></el-option>   
                        </el-select>
                    </el-form-item>
                    <el-form-item label="分组:" prop="groupId" :rules="commonRules.notNullRule">
                        <el-select v-model="busArticle.groupId" clearable>
                            <el-option  :label="group.name" :value="group.id" v-for="(group,index) in groupList" :key="index"></el-option>     
                        </el-select>
                    </el-form-item>
                    <el-form-item label="文章封面:" prop="articleCoverObject.resourceUrl" :rules="commonRules.notNullRule">
                        <el-upload
                                class="avatar-uploader"
                                accept="image/*"
                                :action="upladUrl"
                                name="file"
                                :show-file-list="false"
                                :before-upload="beforeUpload"
                                :on-success="(res)=>{
                            busArticle.articleCoverObject.resourceUrl = replaceImgUrl(res.filePath)
                        }">
                            <img v-if="busArticle.articleCoverObject.resourceUrl" :src="busArticle.articleCoverObject.resourceUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                        <span class="uploadTip">图片大小 718*466</span>   
                    </el-form-item>
                    <el-form-item label="推荐封面:" v-if="busArticle.channelId===2">
                        <el-upload
                                class="avatar-uploader"
                                accept="image/*"
                                :action="upladUrl"
                                name="file"
                                :show-file-list="false"
                                :before-upload="beforeUpload"
                                :on-success="(res)=>{
                            busArticle.recommendCoverObject.resourceUrl = replaceImgUrl(res.filePath)
                        }">
                            <img v-if="busArticle.recommendCoverObject.resourceUrl" :src="busArticle.recommendCoverObject.resourceUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                        <span class="uploadTip" >图片大小 234*280</span>   
                    </el-form-item>
                    <el-form-item label="海报封面:" prop="posterCoverObject.resourceUrl" :rules="commonRules.notNullRule" v-if="busArticle.channelId===4||busArticle.channelId===2">
                        <el-upload
                                class="avatar-uploader"
                                accept="image/*"
                                :action="upladUrl"
                                name="file"
                                :show-file-list="false"
                                :before-upload="beforeUpload"
                                :on-success="(res)=>{
                            busArticle.posterCoverObject.resourceUrl = replaceImgUrl(res.filePath)
                        }">
                            <img v-if="busArticle.posterCoverObject.resourceUrl" :src="busArticle.posterCoverObject.resourceUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                        <span class="uploadTip" >图片大小 720*720</span>   
                    </el-form-item>
                    <el-form-item label="浏览人数:" prop="fakeBrowseNum" :rules="commonRules.numberRule">
                        <el-input v-model="busArticle.fakeBrowseNum" placeholder="请输入浏览人数"></el-input>
                    </el-form-item>
                    <el-form-item label="文章排序:" prop="sort" :rules="commonRules.numberRule">
                        <el-input  v-model="busArticle.sort" placeholder="数字越大越靠前">
                            <template slot="append">已排序至{{articleSort}}</template>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="文章类型:" prop="urlType" :rules="commonRules.notNullRule">
                      <el-radio-group v-model="busArticle.urlType" @change="changeType">
                        <el-radio :label="2" :disabled="busArticle.channelId===4">URL</el-radio>
                        <el-radio :label="1" :disabled="busArticle.channelId===2||busArticle.channelId===3||busArticle.channelId===4">富文本编辑器</el-radio>
                        <el-radio :label="3" :disabled="busArticle.channelId===2||busArticle.channelId===3">普通文本</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item label="跳转URL:" prop="detailUrl" :rules="commonRules.notNullRule" v-if="busArticle.urlType===2">
                        <el-input v-model="busArticle.detailUrl" placeholder="请输入跳转地址"></el-input>
                    </el-form-item>

                    <el-form-item label="文章内容:" prop="busArticleDetail.content" :rules="commonRules.notNullRule" v-if="busArticle.urlType===1">
                        <tinymce v-model="busArticle.busArticleDetail.content" placeholder="请输入文章内容"></tinymce>
                    </el-form-item>
                    <el-form-item label="文章内容:" prop="busArticleDetail.content" :rules="commonRules.notNullRule" v-if="busArticle.urlType===3">
                      <el-input type="textarea" :rows="4" v-model="busArticle.busArticleDetail.content" maxlength="250"></el-input>
                    </el-form-item>
                </div>
                <el-form-item label="" style="margin-top:20px;">
                    <el-button type="primary" size="medium" :loading="btnLoading" @click="save">保存</el-button>
                    <el-button type="info" size="medium" plain @click="$router.push({ name: 'article' })">返回</el-button>
                </el-form-item> 
            </el-form> 
        </section>
    </div>
</template>
<script>
import Tinymce from '@/components/Tinymce'
  export default {
    name: 'articleEdit',
    components: {
      Tinymce
    },
    mounted(){
        this.busArticle.id = this.$route.query.id
        if(this.busArticle.id) {
            this.getConfig()
        }   
    },
    created() {
        this.getSort()
    },
    data(){
        return {
            busArticle: {
                id: '',
                title: '',
                secondTitle: '',
                channelId: '',
                groupId:'',
                //文章封面
                articleCoverObject: {
                    resourceType:1,
                    resourceUrl:''
                },
                //推荐封面
                recommendCoverObject: {
                    resourceType:1,
                    resourceUrl:''
                },
                //海报封面
                posterCoverObject: {
                    resourceType:1,
                    resourceUrl:''
                },
                fakeBrowseNum:'',
                sort:'',
                busArticleDetail:{
                    content:'',
                    articleId:''
                },
                recommendType:0,
                urlType: 2,
                detailUrl: ''
            },
            groupList:[],
            btnLoading: false,
            articleSort:''
        }
    },
    methods: {
      changeType (val) {
        if (val !== 2) {
          this.busArticle.busArticleDetail.content = ''
          this.$nextTick(() => {
            this.$refs.form.clearValidate('busArticleDetail.content')
          })
        }
      },
        getConfig(){
            this.request({
            url: '/bus-article/get?id='+this.busArticle.id,
            method: 'get',
            }).then((res) => {
                if(res.code===200) {
                    this.busArticle = {
                        ...res.data,
                        recommendCoverObject: res.data.recommendCoverObject ? res.data.recommendCoverObject : {
                            resourceType:1,
                            resourceUrl:''
                        },
                        posterCoverObject: res.data.posterCoverObject ? res.data.posterCoverObject : {
                            resourceType:1,
                            resourceUrl:''
                        },
                        busArticleDetail: res.data.busArticleDetail ? res.data.busArticleDetail : {
                            content:'',
                            articleId:''
                        },
                        articleCoverObject: res.data.articleCoverObject ? res.data.articleCoverObject : {
                            resourceType:1,
                            resourceUrl:''
                        }
                    }
                    this.getTagGroup(res.data.channelId)
                }
            }).catch(err => {
               this.$message.success(err.message)
            });
        },
        save() {
            this.btnLoading=true
            this.$refs.form.validate(valid => {
                if(valid) {
                    console.log(this.busArticle)
                    this.request({
                        url: '/bus-article/save',
                        method: 'post',
                        data:this.busArticle
                    }).then((res) => {
                        if(res.code===200) {
                            this.$message.success(res.msg)
                            this.btnLoading=false
                            this.$router.push({ name: 'article' })
                        }
                    }).catch((err)=> {
                        this.btnLoading = false
                    })   
                }     
            })
        },
        uploadImg(event, item) {
            const file = event.target.files[0];
            this.picFileUpload(file, (res) => {
                item.push(this.replaceImgUrl(res.filePath));
            })
        },
        beforeUpload(file) {
            const isLt5M = file.size / 1024 / 1024 < 5
            if (!isLt5M) {
                this.$message.error('上传头像图片大小不能超过 5MB!')
            }
            return isLt5M
        },
        beforeUploadVideo(file) {
            const isLt50M = file.size / 1024 / 1024 < 50
            if (!isLt50M) {
                this.$message.error('上传头像图片大小不能超过 50MB!')
            }
            return isLt50M
        },
        changeChannel (val) {
          this.busArticle.groupId = ''
          this.busArticle.urlType = ''
          this.busArticle.busArticleDetail.content = ''
          this.getTagGroup(val)
        },
        async getTagGroup(channelId) {
            const response = await this.request({
                url: `/bus-group/list?${channelId}`,
                method: 'get',
                params: {
                  channelId: channelId
                }
            }).catch((err)=> {
                this.$message.error(err.msg)
            })
            if(response.code===200){
                this.groupList = response.data
            } 
        },
        async getSort() {
            const articleSort = await this.request({
                url: '/bus-article/sort-last',
                method: 'get'
            }).catch((err)=> {
                this.$message.error(err.msg)
            }) 
            if(articleSort.code===200){
            this.articleSort = articleSort.data
            }
        }
    }   
  }
</script>
<style lang="scss">
    @import "../../../styles/_mixin.scss";
    .uploadTip{
        color: red;
        position: absolute;
        bottom: 3px;
        left: 185px;
    }
    .block-header{
        color: #1ea0ff;
        font-size: 14px;
        padding: 12px 0;
        margin-bottom: 12px;
        background: #f7f7f7;
        &>div{
            padding: 0 12px;
            border-left: 2px solid #1ea0ff;
        }
    }

    .coupon-edit .el-form .el-textarea .el-textarea__inner{
        height: auto;
    }
    .adviceTxt{
        color: #8c939d
    }
    .use-type-block{
        @include flex;
        margin-left: 24px;
    }
    .add-btn-block{
        margin:  0 0 12px 24px;
    }
     .section-content{
        .el-input-group__prepend{
             padding: 0 5px;
         }
        .line{
            text-align: center;
        }
        .el-input{
            width: 100%;
            max-width: 500px;
            min-width: 150px;
        }
        .el-icon-delete{
            color: red;
            font-size: 16px;
            margin: 0 12px;
            cursor: pointer;
        }
        .goods-item{
            padding: 12px 0;
            // border: 1px solid #efefef;
            border-radius: 6px;
            position: relative;

            .avatar-uploader .el-upload,
            .upload-img-list li,
            .upload-img-list .upload-btn{
                width: 120px;
                height: 120px;
                line-height: 118px;
            }
            .upload-img-list .img-preview .img-preview-mask .mask-btn {
                margin-top: 47px;
                line-height: 36px;
                text-align: center;
            }

            .present-item{
                padding: 6px;
                border: 1px solid #efefef;
                margin-bottom: 12px;
                position: relative;

                .el-icon-error{
                    color: red;
                    font-size: 18px;
                    cursor: pointer;
                    top: 12px;
                    right: 12px;
                    position: absolute;
                    z-index: 9;
                }

                &>.el-form-item {
                    margin-bottom: 22px;
                }
                .el-input,
                .el-select{
                    width: 100%;
                    max-width: 160px;
                }
            }
        }
        .add-btn-block{
            margin:  0 0 12px 24px;
        }
        .footer-block{
            text-align: center;
            margin-top: 24px;
        }
    }
</style>
